using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IceAge.Administration
{
    public partial class UserRight : Form
    {
        public UserRight()
        {
            InitializeComponent();
        }
       //private int menu_count = 0;
        private object[] parameters = null;
        private void UserRight_Load(object sender, EventArgs e)
        {
            //CenturyFox.Service data_access = new CenturyFox.Service();
            //string role = CommonLibrary.DoExecute("0101", new object[2] {"hoanvk", "kijuto" }).ToString();
            try
            {
                DataSet menus = CommonLibrary.GetDataSetFromProcedure(
                    "p_sel_SystemMenus", "'M'");

                if (menus != null)
                {
                    foreach (DataRow row in menus.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(row["PARENT_ID"]) == 0)
                        {
                            TreeNode root = new TreeNode();
                            root.Text = row["MENU_TEXT"].ToString();
                            root.Tag = row["MENU_ID"];
                            treeView1.Nodes.Add(root);
                            AddTreeNode(ref root, menus);
                        }
                    }
                }
                //menu_count = menus.Tables[0].Rows.Count;
                treeView1.ExpandAll();
                //Build menu report
                menus = CommonLibrary.GetDataSetFromProcedure("p_sel_SystemMenus", "'R'");

                if (menus != null)
                {
                    foreach (DataRow row in menus.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(row["PARENT_ID"]) == 0)
                        {
                            TreeNode root = new TreeNode();
                            root.Text = row["MENU_TEXT"].ToString();
                            root.Tag = row["MENU_ID"];
                            treeView2.Nodes.Add(root);
                            AddTreeNode(ref root, menus);
                        }
                    }
                }
                //menu_count = menus.Tables[0].Rows.Count;
                treeView2.ExpandAll();
                parameters = (object[])this.Tag;
                this.Text = this.Text + " - " + parameters[0].ToString();
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
            
        }
        private void AddTreeNode(ref TreeNode parent, DataSet menus)
        {
            foreach (DataRow row in menus.Tables[0].Rows)
            {
                if (Convert.ToInt32(row["PARENT_ID"]) == Convert.ToInt32(parent.Tag))
                {
                    TreeNode child = new TreeNode();
                    child.Text = row["MENU_TEXT"].ToString();
                    child.Tag = row["MENU_ID"];
                    parent.Nodes.Add(child);
                    AddTreeNode(ref child, menus);
                }
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            CheckTreeNode(e.Node);            
        }
        private void CheckTreeNode(TreeNode parent)
        {
            foreach (TreeNode child in parent.Nodes)
            {
                child.Checked = parent.Checked;
                CheckTreeNode(child);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //if (dataGridView1.RowCount == 0)
            //    return;
            //string user_name = dataGridView1.CurrentRow.Cells["UserName"].Value.ToString();
            
            //if (MessageBox.Show("Dong y cap lai quyen cho nguoi dung: " + user_name + "?",
            //    this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            //    return;
            //int user_id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["UserId"].Value);
            //UpdateTreeNode(treeView1.Nodes[0], user_id);
            //MessageBox.Show("Cap lai quyen cho nguoi dung: " + user_name + " thanh cong!",
            //    this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void UpdateTreeNode(TreeNode parent, int user_id)
        {
            //CenturyFox.Service data_access = new CenturyFox.Service();
            string status = "S";
            if (parent.Checked)
                status = "A";
            CommonLibrary.ExecDataFromProcedure("p_upd_UserRight", 
                user_id + "," + parent.Tag + ",'" + status +"'"); 
            foreach (TreeNode child in parent.Nodes)
            {                
                UpdateTreeNode(child, user_id);
            }
        }

        private void ShowUserMenus(TreeNode parent, DataSet menus)
        {
            foreach (DataRow row in menus.Tables[0].Rows)
            {
                if (Convert.ToInt32(parent.Tag) == Convert.ToInt32(row["MENU_ID"]))
                {
                    parent.Checked = true;
                    break;
                }
            }
            foreach (TreeNode child in parent.Nodes)
            {
                ShowUserMenus(child, menus);
            }
        }

        private void UserRight_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F11)
            {
                CommitChange();
            }
        }

        private void CommitChange()
        {
            int user_id = Convert.ToInt32(parameters[1]);
            UpdateTreeNode(treeView1.Nodes[0], user_id);
            UpdateTreeNode(treeView2.Nodes[0], user_id);
        }
        private void LoadData()
        {
            string user_name = parameters[0].ToString();

            DataSet menus = CommonLibrary.GetDataSetFromProcedure("p_sel_MenusByUsername", 
                "'" + user_name +"','M'");

            //reset treeview
            foreach (TreeNode node in treeView1.Nodes)
            {
                uncheck_allnodes(node);
            }
            foreach (TreeNode node in treeView2.Nodes)
            {
                uncheck_allnodes(node);
            }
            //end reset

            if (menus != null)
            {
                ShowUserMenus(treeView1.Nodes[0], menus);
            }

            menus = CommonLibrary.GetDataSetFromProcedure("p_sel_MenusByUsername",
                "'" + user_name + "','R'");
            if (menus != null)
            {
                ShowUserMenus(treeView2.Nodes[0], menus);
            }
        }

        private void uncheck_allnodes(TreeNode nodes)
        {
            nodes.Checked = false;
            foreach (TreeNode node in nodes.Nodes)
            {
                uncheck_allnodes(node);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            CommitChange();
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}